package com.example.hongbao;

import java.util.Random;

/**
 * @author zhy
 * @version 1.0.0
 * @ClassName HongBao.java
 * @Description 红包算法
 * @createTime 2021年04月22日 11:00:00
 */
public class HongBao {

    public static double getRandomMoney(RedPackage redPackage) {
        // remainSize 剩余红包数量
        // remainMoney 剩余钱
        if (redPackage.remainSize == 1) {
            redPackage.remainSize--;
            return (double) Math.round(redPackage.remainMoney * 100) / 100;
        }
        Random r = new Random();
        double min = 0.01;
        double max = redPackage.remainMoney / redPackage.remainSize * 2;
        double money = r.nextDouble() * max;
        money = money <= min ? 0.01 : money;
        money = Math.floor(money * 100) / 100;
        redPackage.remainSize--;
        redPackage.remainMoney -= money;
        return money;
    }

    static class RedPackage {
        private int remainSize;
        private double remainMoney;

        public RedPackage(int remainSize, int remainMoney) {
            this.remainSize = remainSize;
            this.remainMoney = remainMoney;
        }
    }

    public static void main(String[] args) {
        HongBao.RedPackage redPackage = new HongBao.RedPackage(30, 500);
        double total = 0.00;
        while (redPackage.remainSize != 0) {
            double money = HongBao.getRandomMoney(redPackage);
            total += money;
            System.out.println(money);
        }
        System.out.println("total=" + total);
    }
}
